package com.tronsis.library.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tronsis.library.R;
import com.tronsis.library.widget.LoadingProgressDialog;

import butterknife.ButterKnife;


/**
 * 带标题栏的activity基类
 * @author aiden@tronsis.com
 * @date 2018/6/7 12:15
 */
public class ToolBarBaseActivity extends BaseActivity implements View.OnClickListener {

    protected View main_view;
    private LinearLayout mLlMainContent, mLlErrorLayout;
    protected ImageButton mIBtnBack,mIBtnLeft, mIBtnRight, mIBtnRightOther;
    protected TextView mTvLeft, mTvTitle, mTvRight, mTvWaterMark,mTvUnRead;
    protected RelativeLayout mRlToolbar;
    protected Toolbar mToolbar;
    protected RelativeLayout mRlRead;
    private LoadingProgressDialog mLoadingProgressDialog;

    public static final int LEFT_BUTTON_BACK = 1;
    public static final int RIGHT_BUTTON = 2;
    public static final int LEFT_TEXT = 3;
    public static final int RIGHT_TEXT = 4;
    public static final int RIGHT_BUTTON_OTHER = 5;
    public static final int LEFT_BUTTON = 6;
    protected Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        mContext = this;
        main_view = findViewById(R.id.main_view);
        mIBtnBack = (ImageButton) findViewById(R.id.ibtn_back);
        mIBtnLeft = (ImageButton) findViewById(R.id.ibtn_left);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mIBtnRight = (ImageButton) findViewById(R.id.ibtn_right);
        mIBtnRightOther = (ImageButton) findViewById(R.id.ibtn_right_other);
        mTvLeft = (TextView) findViewById(R.id.tv_left);
        mTvRight = (TextView) findViewById(R.id.tv_right);
//        mRlToolbar = (RelativeLayout) findViewById(R.id.rl_toolbar);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mTvWaterMark = (TextView) findViewById(R.id.tv_watermark);
        mRlRead =  findViewById(R.id.rl_read);
        mTvUnRead =  findViewById(R.id.tv_unread);
        mIBtnRight.setOnClickListener(this);
        mIBtnLeft.setOnClickListener(this);
        mIBtnBack.setOnClickListener(this);
        mTvLeft.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
        mIBtnRightOther.setOnClickListener(this);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        setContentView(View.inflate(this, layoutResID, null));
        setStatusBarUpperAPI21();
        ButterKnife.bind(this);

    }

    @Override
    public void setContentView(View view) {
        mLlMainContent = (LinearLayout) findViewById(R.id.main_content);
//        mLlErrorLayout = (LinearLayout) findViewById(R.id.ll_load_fail);
        if(mLlMainContent == null){
            return;
        }
        mLlMainContent.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
     }

    protected void hideToolbar() {
        mToolbar.setVisibility(View.GONE);
    }

    protected void setTitle(String title) {
        setTitle(false, title);
    }

    protected void setTitle(boolean showCancel, String title) {
        setTitle(showCancel, title, -1);
    }

    protected void setTitle(boolean showCancel, String title, int rightResId) {
        if(showCancel) {
            mIBtnBack.setVisibility(View.VISIBLE);
        }
        mTvTitle.setText(title);
        if(rightResId != -1) {
            mIBtnRight.setImageResource(rightResId);
            mIBtnRight.setVisibility(View.VISIBLE);
        }
    }

    protected void setTitle(boolean showCancel, String title, String rightText) {
        if(showCancel) {
            mIBtnBack.setVisibility(View.VISIBLE);
        }
        mTvTitle.setText(title);
        if(!TextUtils.isEmpty(rightText)) {
            mTvRight.setText(rightText);
            mTvRight.setVisibility(View.VISIBLE);
        }
    }

    protected void setTitle(String leftText, String title, int rightResId) {
        if(!TextUtils.isEmpty(leftText)) {
            mTvLeft.setText(leftText);
            mTvLeft.setVisibility(View.VISIBLE);
        }
        mTvTitle.setText(title);
        if(rightResId != -1) {
            mIBtnRight.setImageResource(rightResId);
            mIBtnRight.setVisibility(View.VISIBLE);
        }
    }

    protected void setTitle(String leftText, String title) {
        setTitle(leftText, title, "");
    }

    protected void setTitle(String leftText, String title, String rightText) {
        if(!TextUtils.isEmpty(leftText)) {
            mTvLeft.setText(leftText);
            mTvLeft.setVisibility(View.VISIBLE);
        }
        mTvTitle.setText(title);
        if(!TextUtils.isEmpty(rightText)) {
            mTvRight.setText(rightText);
            mTvRight.setVisibility(View.VISIBLE);
        }
    }

    private int getStatusHeight() {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.ibtn_back) {
            onToolbarClicked(LEFT_BUTTON_BACK);
            finish();

        } if (i == R.id.ibtn_left) {
            onToolbarClicked(LEFT_BUTTON);

        } else if (i == R.id.tv_left) {
            onToolbarClicked(LEFT_TEXT);

        } else if (i == R.id.ibtn_right) {
            onToolbarClicked(RIGHT_BUTTON);

        } else if (i == R.id.tv_right) {
            onToolbarClicked(RIGHT_TEXT);

        } else if (i == R.id.ibtn_right_other) {
            onToolbarClicked(RIGHT_BUTTON_OTHER);

        }
    }



    protected void onToolbarClicked(int paramId) {
    }

    protected void showErrorLayout() {
        if (mLlErrorLayout.isShown()) {
            return;
        }
        mLlErrorLayout.setVisibility(View.VISIBLE);
        mLlMainContent.getChildAt(2).setVisibility(View.GONE);
    }

    protected void showContent() {
        if (mLlErrorLayout.isShown()) {
            mLlErrorLayout.setVisibility(View.GONE);
            mLlMainContent.getChildAt(2).setVisibility(View.VISIBLE);
        }
    }

    protected void setToolbarColor(int color){
        mToolbar.setBackgroundColor(color);
    }

    protected void setRightTvText(String string){
        mTvRight.setText(string);
        mTvRight.setVisibility(View.VISIBLE);
    }

    protected void showLoadingDialog() {
        showLoadingDialog("");
    }


}
